package com.jtjrenren.android.taxi.driver.core.other;

import android.content.Intent;
import android.content.res.AssetManager;
import android.view.View;
import android.widget.Button;

import com.jtjrenren.android.taxi.driver.bean.CityModel;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.DistrictModel;
import com.jtjrenren.android.taxi.driver.bean.ProvinceModel;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.utils.XmlParserHandler;
import com.library.weiget.wheel.OnWheelChangedListener;
import com.library.weiget.wheel.WheelView;
import com.library.weiget.wheel.adapters.ArrayWheelAdapter;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by Administrator on 2016/5/16.
 */
public class selectCityDActivity extends BaseActivity implements View.OnClickListener, OnWheelChangedListener {
    private WheelView wv_provicence, wv_city;
    private Button bt_cancle, bt_ok;

    protected String[] mProvinceDatas;
    protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
    protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();
    protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();

    protected String mCurrentProviceName;
    protected String mCurrentCityName;
    protected String mCurrentDistrictName = "";
    protected String mCurrentZipCode = "";

    @Override
    public int getLayoutID() {
        return R.layout.daty_select_city;
    }

    @Override
    public void init() {
        findViews();
        setListeners();

        initProvinceDatas();
        wv_provicence.setViewAdapter(new ArrayWheelAdapter<String>(this, mProvinceDatas));
        // 设置可见条目数量
        wv_provicence.setVisibleItems(7);
        wv_city.setVisibleItems(7);

        updateCities();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wv_provicence) {
            updateCities();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancle:
                finish();
                break;
            case R.id.ok:
                setResult();
                break;
        }
    }

    private void setResult() {
        String p = mProvinceDatas[wv_provicence.getCurrentItem()];
        String c = mCitisDatasMap.get(p)[wv_city.getCurrentItem()];
        Intent data = new Intent();
        data.putExtra("p", p);
        data.putExtra("c", c);
        setResult(RESULT_OK, data);
        finish();
    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    private void updateCities() {
        int pCurrent = wv_provicence.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        wv_city.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        wv_city.setCurrentItem(0);
    }

    protected void initProvinceDatas() {
        List<ProvinceModel> provinceList = null;
        AssetManager asset = getAssets();
        try {
            InputStream input = asset.open("province_data.xml");
            // 创建一个解析xml的工厂对象
            SAXParserFactory spf = SAXParserFactory.newInstance();
            // 解析xml
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            // 获取解析出来的数据
            provinceList = handler.getDataList();
            //*/ 初始化默认选中的省、市、区
            if (provinceList != null && !provinceList.isEmpty()) {
                mCurrentProviceName = provinceList.get(0).getName();
                List<CityModel> cityList = provinceList.get(0).getCityList();
                if (cityList != null && !cityList.isEmpty()) {
                    mCurrentCityName = cityList.get(0).getName();
                    List<DistrictModel> districtList = cityList.get(0).getDistrictList();
                    mCurrentDistrictName = districtList.get(0).getName();
                    mCurrentZipCode = districtList.get(0).getZipcode();
                }
            }
            //*/
            mProvinceDatas = new String[provinceList.size()];
            for (int i = 0; i < provinceList.size(); i++) {
                // 遍历所有省的数据
                mProvinceDatas[i] = provinceList.get(i).getName();
                List<CityModel> cityList = provinceList.get(i).getCityList();
                String[] cityNames = new String[cityList.size()];
                for (int j = 0; j < cityList.size(); j++) {
                    // 遍历省下面的所有市的数据
                    cityNames[j] = cityList.get(j).getName();
                    List<DistrictModel> districtList = cityList.get(j).getDistrictList();
                    String[] distrinctNameArray = new String[districtList.size()];
                    DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
                    for (int k = 0; k < districtList.size(); k++) {
                        // 遍历市下面所有区/县的数据
                        DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        // 区/县对于的邮编，保存到mZipcodeDatasMap
                        mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        distrinctArray[k] = districtModel;
                        distrinctNameArray[k] = districtModel.getName();
                    }
                    // 市-区/县的数据，保存到mDistrictDatasMap
                    mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
                }
                // 省-市的数据，保存到mCitisDatasMap
                mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }

    private void setListeners() {
        wv_provicence.addChangingListener(this);
        wv_city.addChangingListener(this);
        bt_ok.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
    }

    private void findViews() {
        wv_provicence = (WheelView) findViewById(R.id.provience);
        wv_city = (WheelView) findViewById(R.id.city);

        bt_cancle = (Button) findViewById(R.id.cancle);
        bt_ok = (Button) findViewById(R.id.ok);
    }


}
